﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ComicWeb.Models;

namespace ComicWeb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserInfoesController : ControllerBase
    {
        private readonly ShopContext _context;
        //注入依赖
        public UserInfoesController(ShopContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 登录API
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<string>> PostUserInfos(UserInfo us)
        {
            var data = _context.UserInfos.FirstOrDefault(m => m.Phone == us.Phone && m.UserPwd == us.UserPwd);
            if (data == null)
            {
                return "-1";
            }
            var id = data.UserId.ToString();
            return id;
        }
        /// <summary>
        /// 注册API
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<bool>> PostSign(UserInfo us)
        {
            var data = _context.UserInfos.FirstOrDefault(m => m.Phone == us.Phone);
            if (data == null)
            {
                _context.Entry(us).State = EntityState.Modified;
                _context.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
